package com.zc.manager.rule.engine.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.exception.job.TaskException;
import com.ruoyi.quartz.util.CronUtils;
import com.zc.manager.rule.engine.domain.IotJob;
import com.zc.manager.rule.engine.service.IIotJobService;
import org.quartz.SchedulerException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 任务调度Controller
 *
 * @author wangjiabao
 * @date 2023-02-22
 */
@RestController
@RequestMapping("/rule-engine/job")
public class IotJobController extends BaseController
{
    @Resource
    private IIotJobService iotJobService;

    /**
     * 查询定时任务列表
     */
    @PreAuthorize("@ss.hasPermi('rule-engine:job:list')")
    @GetMapping("/list")
    public TableDataInfo list(IotJob iotJob)
    {
        startPage();
        List<IotJob> list = iotJobService.selectJobList(iotJob);
        return getDataTable(list);
    }

    /**
     * 导出定时任务列表
     */
    @PreAuthorize("@ss.hasPermi('rule-engine:job:export')")
    @Log(title = "定时任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotJob iotJob)
    {
        List<IotJob> list = iotJobService.selectJobList(iotJob);
        ExcelUtil<IotJob> util = new ExcelUtil<IotJob>(IotJob.class);
        util.exportExcel(response, list, "定时任务");
    }

    /**
     * 获取定时任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('rule-engine:job:query')")
    @GetMapping(value = "/{jobId}")
    public AjaxResult getInfo(@PathVariable("jobId") Long jobId)
    {
        return AjaxResult.success(iotJobService.selectJobById(jobId));
    }

    /**
     * 新增定时任务
     */
    @PreAuthorize("@ss.hasPermi('rule-engine:job:add')")
    @Log(title = "定时任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IotJob job) throws SchedulerException, TaskException
    {
        if (!CronUtils.isValid(job.getCronExpression()))
        {
            return error("新增任务'" + job.getJobName() + "'失败，Cron表达式不正确");
        }
        job.setCreateBy(getUsername());
        return toAjax(iotJobService.insertJob(job));
    }

    /**
     * 修改定时任务
     */
    @PreAuthorize("@ss.hasPermi('rule-engine:job:edit')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotJob job) throws SchedulerException, TaskException
    {
        if (!CronUtils.isValid(job.getCronExpression()))
        {
            return error("修改任务'" + job.getJobName() + "'失败，Cron表达式不正确");
        }
        job.setUpdateBy(getUsername());
        return toAjax(iotJobService.updateJob(job));
    }

    /**
     * 定时任务状态修改
     */
    @PreAuthorize("@ss.hasPermi('rule-engine:job:changeStatus')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody IotJob job) throws SchedulerException
    {
        IotJob newJob = iotJobService.selectJobById(job.getJobId());
        newJob.setStatus(job.getStatus());
        return toAjax(iotJobService.changeStatus(newJob));
    }

    /**
     * 定时任务立即执行一次
     */
    @PreAuthorize("@ss.hasPermi('rule-engine:job:changeStatus')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/run")
    public AjaxResult run(@RequestBody IotJob job) throws SchedulerException
    {
        iotJobService.run(job);
        return AjaxResult.success();
    }

    /**
     * 删除定时任务
     */
    @PreAuthorize("@ss.hasPermi('rule-engine:job:remove')")
    @Log(title = "定时任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jobIds}")
    public AjaxResult remove(@PathVariable Long[] jobIds) throws SchedulerException, TaskException
    {
        iotJobService.deleteJobByIds(jobIds);
        return AjaxResult.success();
    }

    /**
     * 定时任务绑定
     */
    @PreAuthorize("@ss.hasPermi('rule-engine:job:changeStatus')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/bind/{jobId}")
    public AjaxResult bind(@PathVariable Long jobId, @RequestBody String invokeTarget) throws SchedulerException, TaskException
    {
        return toAjax(iotJobService.bind(jobId, invokeTarget));
    }
}
